package com.ruoyi.patrol.quartzTask;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.patrol.domain.PtLineGuard;
import com.ruoyi.patrol.domain.PtLineStaff;
import com.ruoyi.patrol.domain.PtLineTask;
import com.ruoyi.patrol.service.IPtLineGuardService;
import com.ruoyi.patrol.service.IPtLineStaffService;
import com.ruoyi.patrol.service.IPtLineTaskService;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ：小杜
 * @date ：Created in 2023/11/28 14:10
 * @描述 ：
 */
@Component("lineTask")
public class LineTask {
    private static IPtLineStaffService ptLineStaffService;
    private static IPtLineGuardService ptLineGuardService;
    private static IPtLineTaskService ptLineTaskService;

    public static void setContext(ConfigurableApplicationContext context) {
        ptLineStaffService = context.getBean(IPtLineStaffService.class);
        ptLineGuardService = context.getBean(IPtLineGuardService.class);
        ptLineTaskService = context.getBean(IPtLineTaskService.class);
    }

    public static void insertLineTask() {
        System.out.println("定时排班开始执行");
        List<PtLineTask> ptLineTasks = ptLineTaskService.selectPtLineTaskList(new PtLineTask());
        if (ptLineTasks.isEmpty()) {
            System.out.println("暂无定时排班任务");
            return;
        }
        for (PtLineTask ptLineTask : ptLineTasks) {
            // 0表示保安 1表示行政
            if ("0".equals(ptLineTask.getTaskType())) {
                //插入保安排班
                PtLineGuard ptLineGuard = new PtLineGuard();
                ptLineGuard.setLineId(ptLineTask.getLineId());
                ptLineGuard.setGroupId(ptLineTask.getGroupId());
                ptLineGuard.setDate(DateUtils.parseDate(DateUtils.getDate()));
//                ptLineGuard.setDate(DateUtils.parseDate("2023-12-06"));
                long count = ptLineGuardService.selectCount(ptLineGuard);
                if (count < 1) {
                    ptLineGuard.setDate(null);
                    ptLineGuard.setDates(new String[]{DateUtils.getDate()});
//                    ptLineGuard.setDates(new String[]{"2023-12-06"});
                    int i = ptLineGuardService.insertPtLineGuard(ptLineGuard);
                    System.out.println("安保定时排班执行完成:" + i);
                } else {
                    System.out.println("安保定时排班执行（今日已分配）");
                }
            } else {
                //插入行政排班
                PtLineStaff ptLineStaff = new PtLineStaff();
                ptLineStaff.setLineId(ptLineTask.getLineId());
                ptLineStaff.setGroupId(ptLineTask.getGroupId());
                ptLineStaff.setDate(DateUtils.parseDate(DateUtils.getDate()));
//                ptLineStaff.setDate(DateUtils.parseDate("2023-12-06"));
                long count = ptLineStaffService.selectCount(ptLineStaff);
                if (count < 1) {
                    ptLineStaff.setDate(null);
                    ptLineStaff.setDates(new String[]{DateUtils.getDate()});
//                    ptLineStaff.setDates(new String[]{"2023-12-06"});
                    int i = ptLineStaffService.insertPtLineStaff(ptLineStaff);
                    System.out.println("行政定时排班执行完成:" + i);
                } else {
                    System.out.println("行政定时排班执行（今日已分配）");
                }
            }
        }
    }
}
